package com.imis.base.aspect;

import com.imis.module.system.model.po.SysLog;
import com.imis.module.system.model.vo.SysUserVO;
import com.imis.module.system.service.ISysLogService;
import com.imis.base.util.IPUtils;
import com.imis.base.util.SpringContextUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * SysLogAspect<br>
 * 系统日志，切面处理类
 * </p>
 *
 * @author XinLau
 * @version 1.0
 * @since 2020年03月24日 14:37
 */
@Aspect
@Component
public class SysLogAspect {

    private ISysLogService sysLogService;

    @Autowired
    public void setSysLogService(ISysLogService sysLogService) {
        this.sysLogService = sysLogService;
    }

    @Pointcut("@annotation(com.imis.base.annotation.SysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        //保存日志
        saveSysLog(point, time);

        return result;
    }

    /**
     * 保存日志
     *
     * @param joinPoint -
     * @param time      - 执行时间
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/6/19 8:48
     */
    private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLog sysLogEntity = new SysLog();
        com.imis.base.annotation.SysLog syslog = method.getAnnotation(com.imis.base.annotation.SysLog.class);
        if (syslog != null) {
            // 注解上的描述
            sysLogEntity.setLogContent(syslog.value());
        }

        // 请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLogEntity.setMethod(className + "." + methodName + "()");

        // 请求的参数
        Object[] args = joinPoint.getArgs();
        String params = Arrays.toString(args);
        sysLogEntity.setRequestParam(params);

        // 获取request
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        // 设置IP地址
        sysLogEntity.setIp(IPUtils.getClientIpAddress(request));

        // 用户名
        String username = ((SysUserVO) SecurityUtils.getSubject().getPrincipal()).getUsername();
        sysLogEntity.setUsername(username);

        sysLogEntity.setCostTime(time);
        sysLogEntity.setCreateTime(LocalDateTime.now());
        // 保存系统日志
        sysLogService.save(sysLogEntity);
    }

}
